package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.PageResponse;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Kongwo
 * @description
 * @since 2022/05/19 09:00
 */

@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Autowired
    GlobalIdGeneratorUtil globalIdGeneratorUtil;

    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;

    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {

        long uid = getUid(servletRequest);
        request.setUserId(uid);

        request.requestCheck();
        request.setUniqueKey(globalIdGeneratorUtil.getNextSeq(OrderConstants.ORDER_ID, 1));
        CreateOrderResponse response = orderCoreService.createOrder(request);

        // 如果执行不成功，可能是因为没往里面放success的code
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }

    @GetMapping("/order")
    public ResponseData getOrderList(OrderListRequest request, HttpServletRequest servletRequest) {
        long uid = getUid(servletRequest);
        request.setUserId(uid);

        // 对请求进行校验
        request.requestCheck();
        OrderListResponse response = orderQueryService.orderList(request);
        PageResponse pageResponse = new PageResponse();
        pageResponse.setData(response.getDetailInfoList());
        pageResponse.setTotal(response.getTotal());

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            if (response.getTotal() == 0) {
                return new ResponseUtil<>().setData(pageResponse);
            }
            return new ResponseUtil<>().setData(pageResponse);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }

    @GetMapping("/order/{orderId}")
    public ResponseData getOrderDetail(OrderDetailRequest request) {

        request.requestCheck();

        OrderDetailResponse response = orderQueryService.orderDetail(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }


    @PostMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest request) {

        request.requestCheck();

        CancelOrderResponse response = orderCoreService.cancelOrder(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/order/{orderId}")
    public ResponseData deleteOrder(DeleteOrderRequest request) {

        request.requestCheck();

        DeleteOrderResponse response = orderCoreService.deleteOrder(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }

    private long getUid(HttpServletRequest servletRequest) {
        String userInfo = servletRequest.getHeader(OrderConstants.USER_INFO_KEY);

      JSONObject jsonObject = JSON.parseObject(userInfo);
        return jsonObject.getLong("uid");


    }

}
